package com.example.studentdemo.controller;

import com.example.studentdemo.annotation.RequiresRole;
import com.example.studentdemo.common.Result;
import com.example.studentdemo.entity.Task;
import com.example.studentdemo.constant.RoleConstant;
import com.example.studentdemo.service.TaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/task")
@Tag(name = "任务管理", description = "学习任务相关操作接口")
public class TaskController {
    @Resource
    private TaskService taskService;

    @GetMapping
    @Operation(summary = "查询所有任务", description = "查询所有任务接口")
    @Parameter(description = "查询条件", name = "task", required = false)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result selectAll(Task task) {
        List<Task> list = taskService.selectAll(task);
        return Result.success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询任务", description = "分页查询任务接口")
    @Parameter(description = "页码", name = "pageNum", required = true)
    @Parameter(description = "每页数量", name = "pageSize", required = true)
    @Parameter(description = "查询条件", name = "task", required = false)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result selectPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, Task task) {
        return Result.success(taskService.selectPage(pageNum, pageSize, task));
    }

    @PostMapping
    @Operation(summary = "添加任务", description = "添加任务接口")
    @Parameter(description = "任务信息", name = "task", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result add(@RequestBody Task task) {
        taskService.add(task);
        return Result.success();
    }

    @PutMapping
    @Operation(summary = "修改任务", description = "修改任务接口")
    @Parameter(description = "任务信息", name = "task", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result update(@RequestBody Task task) {
        taskService.update(task);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除任务", description = "删除任务接口")
    @Parameter(description = "任务ID", name = "id", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result deleteById(@PathVariable Integer id) {
        taskService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除任务", description = "批量删除任务接口")
    @Parameter(description = "任务列表", name = "taskList", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result deleteBatch(@RequestBody List<Task> taskList) {
        taskService.deleteBatch(taskList);
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询任务", description = "根据ID查询任务接口")
    @Parameter(description = "任务ID", name = "id", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result selectById(@PathVariable Integer id) {
        return Result.success(taskService.selectById(id));
    }

    @GetMapping("/subject/{subjectId}")
    @Operation(summary = "根据课程ID查询任务", description = "根据课程ID查询任务接口")
    @Parameter(description = "课程ID", name = "subjectId", required = true)
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE, RoleConstant.STUDENT_ROLE})
    public Result selectBySubjectId(@PathVariable Integer subjectId) {
        return Result.success(taskService.selectBySubjectId(subjectId));
    }
}